let dataHome = {
	schedule:'',
	scheduleBtn: true,
	items: [
		{id: 1, name: "路由策略和axios发送请求", check: true},
		{id: 2, name: "Echart动态疫情地图", check: false},
		{id: 3, name: 'axios数据交互及全选扩展', check: false},
		{id: 4, name: '树目录无限递归实现', check: false},
		{id: 5, name: 'Vue 实现二级导航目录', check: false},
	],
	newItems:[],
	curType : 1
}

window.onload = function(){
	let HomePage = new Vue({
		el: '.home',
		data: dataHome,
		methods: {
			add:function(){
				// alert("添加计划成功");
				// 尾插法
				// this.items.push({id:this.getNextIndex(), name:this.schedule, check: false});
				// if(this.schedule === '') { return; }
				let nextId = this.getNextIndex();
				this.items.unshift({id : nextId, name: this.schedule, check: false });// 头插法
				this.schedule = '';// 清空
			},
			getNextIndex() {
				let next = 0;
				this.items.forEach(v => {if(v.id > next) next = v.id;});
				return next + 1;
			},
			delItem(item){
				let index = this.items.findIndex(v => v.id === item.id)
				console.log(index)
				this.items.splice(index , 1);
				this.filterItem(this.curType);
			},
			filterItem(type){
				// if(type === 2) { this.newItems = this.items.filter(item => item.check); } 
				// else if(type === 3) {this.newItems = this.items.filter(item => !item.check);} 
				// else {this.newItems = items; }
				this.curType = type;
				switch(type) {
					case 2 : this.newItems = this.items.filter(item => item.check); break;
					case 3 :	this.newItems = this.items.filter(item => !item.check); break;
					default: this.newItems = this.items;
				}
			},
			addSelected(id){
				this.items.forEach(v => {
					let _check = v.check;
					if(v.id == id) {
						v.check = !_check;
					}
				});
			}
		},
		mounted() {
			this.filterItem(this.curType);
		},
		computed: {
			// 闭包保活
			completed(tag){
				return function(tag){
					let hasCompleted = 0;
					if(tag === "undefined") { return this.items.length; }
					this.items.forEach(v => { if(tag === v.check) { hasCompleted +=1; }  });
					return hasCompleted;
				}
			}
		},
		created:function(){
			// 需注意 function 内部this指向的就是方法对象
			var _this = this;
			window.onkeydown = function(e){
				if(e.keyCode === 13) {
					_this.add();
				}
			}
		}
	})
	
	let SecondPage = new Vue({
		
		
		
	});
	
};